package com.itmk.web.ai.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itmk.HeHeXinXiApi.api.TextInUtil;
import com.itmk.utils.JWTUtils;
import com.itmk.utils.Result;
import com.itmk.web.ai.service.AiService;
import com.itmk.web.companyInfo.entity.JobInfo;
import com.itmk.web.userInfo.entity.*;
//import com.itmk.zhipuapi.demo.V4OkHttpClientTest;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@ResponseBody
@RequestMapping("/api")
public class iicontroller {

    @Autowired
    private AiService aiService;

    @RequestMapping("/searchImage")
    public Result meyh(HttpServletRequest request,MultipartFile resume) {
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        // 检查目录是否存在，如果不存在则创建
        String pre1 = "E:\\file\\pdfs\\resume\\";
        File directory = new File(pre1);
        if (!directory.exists()) {
            directory.mkdirs();
        }


        //接收简历pdf
        //对pdf进行重命名并存储到项目中
        String filePDF = null;
        if(resume!=null){

            //判断图片是否存在
            String exist = aiService.checkFile(uuid);
            if (exist!=null) {
                String pre = "E:\\file\\pdfs\\resume\\";
                File file = new File(pre+exist);
                if(file.delete()){
                    System.out.println("删除成功");
                }else{
                    System.out.println("删除失败");
                    return Result.error("替换原有图片操作失败");
                }
            }

            //file重命名
            String originalFilename = resume.getOriginalFilename();
            String ext = "."+originalFilename.split("\\.")[1];
            String imageUUID = UUID.randomUUID().toString().replace("-","");
            String fileName = imageUUID+ext;
            filePDF = fileName;
            //上传图片
//            ApplicationHome applicationHome = new ApplicationHome(this.getClass());
//            String pre = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath()+
//                    "\\src\\main\\resources\\static\\images\\companyLogo\\";
//            String property = System.getProperty("user.dir")+"\\itmk-base-web";
//            String pre = property + "\\target\\classes\\static\\pdfs\\";

            String pre = "E:\\file\\pdfs\\resume\\";
            aiService.updateResume(fileName,uuid);
            String path1 = pre+fileName;
            String path = path1.replace("/","\\");
            try {
                resume.transferTo(new File(path));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        if(filePDF!=null){
            JSONObject a;
            try {
                //将pdfs名字传给函数
                a = TextInUtil.sendRequest(filePDF);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            JSONObject resultObject = a.getJSONObject("result");
            JSONArray jsonArray = resultObject.getJSONArray("lines");
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject lineObject = jsonArray.getJSONObject(i);
                String text = lineObject.getString("text");
                System.out.println("Text: " + text);
            }
        }
        return Result.error("图片传送失败");
    }
}
